package com.youlai.boot.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.youlai.boot.common.model.Option;
import com.youlai.boot.system.converter.DictDataConverter;
import com.youlai.boot.system.mapper.DictDataMapper;
import com.youlai.boot.system.model.entity.SysDictDataEntity;
import com.youlai.boot.system.model.form.DictDataForm;
import com.youlai.boot.system.model.query.DictDataPageQuery;
import com.youlai.boot.system.model.vo.DictDataPageVO;
import com.youlai.boot.system.service.DictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 字典数据实现类
 *
 * @author haoxr
 * @since 2022/10/12
 */
@Service
@RequiredArgsConstructor
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, SysDictDataEntity> implements DictDataService {

    private final DictDataConverter dictDataConverter;

    /**
     * 获取字典数据分页列表
     *
     * @param queryParams /
     * @return /
     */
    @Override
    public Page<DictDataPageVO> getDictDataPage(DictDataPageQuery queryParams) {
        Page<DictDataPageVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());

        QueryWrapper query = QueryWrapper.create().from(SysDictDataEntity.class)
                .and(SysDictDataEntity::getDictCode).eq(queryParams.getDictCode(), StrUtil.isNotBlank(queryParams.getDictCode()));

        if (StrUtil.isNotBlank(queryParams.getKeywords())) {
            query.and(q -> {
                q.and(SysDictDataEntity::getValue).like(queryParams.getKeywords())
                        .or(SysDictDataEntity::getLabel).like(queryParams.getKeywords());
            });
        }

        return this.pageAs(
                page,
                query,
                DictDataPageVO.class
        );
    }

    /**
     * 获取字典数据表单
     *
     * @param id 字典数据ID
     * @return
     */
    @Override
    public DictDataForm getDictDataForm(Long id) {
        SysDictDataEntity entity = this.getById(id);
        return dictDataConverter.toForm(entity);
    }

    /**
     * 保存字典数据
     *
     * @param formData
     * @return
     */
    @Override
    public boolean saveDictData(DictDataForm formData) {
        SysDictDataEntity entity = dictDataConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新字典数据
     *
     * @param formData 字典数据表单
     * @return
     */
    @Override
    public boolean updateDictData(DictDataForm formData) {
        SysDictDataEntity entity = dictDataConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除字典数据
     *
     * @param ids 字典数据ID集合
     */
    @Override
    public void deleteDictDataByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        this.removeByIds(idList);
    }

    /**
     * 获取字典数据列表
     *
     * @param dictCode 字典编码
     * @return
     */
    @Override
    public List<Option<String>> getDictDataList(String dictCode) {
        return this.list(
                        QueryWrapper.create().from(SysDictDataEntity.class)
                                .select(
                                        QueryMethods.column(SysDictDataEntity::getValue),
                                        QueryMethods.column(SysDictDataEntity::getLabel),
                                        QueryMethods.column(SysDictDataEntity::getTagType)
                                )
                                .eq(SysDictDataEntity::getDictCode, dictCode)
                                .eq(SysDictDataEntity::getStatus, 1)
                                .orderBy(SysDictDataEntity::getSort).asc()
                ).stream().map(item -> new Option<>(item.getValue(), item.getLabel(), item.getTagType()))
                .toList();
    }
}




